import { ref, onMounted } from "vue"
import { getCategoryAPI } from "@/apis/category"
import { useRoute } from "vue-router"
import { onBeforeRouteUpdate } from "vue-router";

export function useCategory() {
    const categoryData = ref({})
    const route = useRoute()
    const getCategory = async(id = route.params.id) => {
        const res = await getCategoryAPI(id)
        categoryData.value = res.result
    }
    onMounted(() => {
            getCategory()
        })
        // 当路由更新时，调用 getCategory 函数，传入新的路由参数 id
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id)

    })
    return { categoryData }
}